import Entity.Courses;
import Entity.CreatEntityManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Ccourse",urlPatterns = {"/course"})
public class CreatCourses extends HttpServlet {
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = CreatEntityManager.GetEntityManager();
        EntityTransaction tx = em.getTransaction();
        Courses Ca = new Courses("1", "数字图像处理", 3, "刘文果");
        Courses Cb = new Courses("2", "C语言设计", 5, "肖捷");
        try {
            tx.begin();
            em.persist(Ca);
            em.persist(Cb);
            tx.commit();
        } catch (Exception e) {

        } finally {
            em.close();
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("插入课程成功");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
